package com.rfxj.cmp.pressurepipe.common;

import com.rfxj.cmp.pressurepipe.autoconfigure.CmpPipeServerProperties;
import com.richfit.ip.utils.WsInvoke;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultAttribute;
import org.dom4j.tree.DefaultElement;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 压力管道Webservice 服务
 */
@Data
public class PipeServiceHelper {
    @Autowired
    private CmpPipeServerProperties pipeServerProperties;
    /**
     * 服务地址
     */
    private  String uri="http://10.79.1.204/services/EquipQueryService?username=wlw&password=zf@123456";
    private  String capsule="<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ser=\"http://service.tlm.webservice.creatsoft.com/\">   <soap:Header/>   <soap:Body> %s </soap:Body> </soap:Envelope>";

    public PipeServiceHelper() {
    }
    /**
     * 构造
     * @param uri 服务地址
     */
    public PipeServiceHelper(String uri){
        if(StringUtils.isNotEmpty(uri)){
            this.setUri(uri);
        }
    }


    /**
     * 调用server
     * @param soapHeader
     * @exception <ser:getPipe> <arg0> &lt;creatsoft&gt;&lt;param name="管道编号" value="TH-HF-NS-GC-0107" /&gt;&lt;/creatsoft&gt;</arg0> </ser:getPipe>
     * @return
     * @throws Exception
     */
    public List<Map<String,String>> invokeService(String soapHeader) throws Exception{
        HashMap<String,String> map=new HashMap<>();
        //System.out.println( String.format(capsule,soapHeader));
        map.put("SOAPHeader", String.format(capsule,soapHeader));
        String xml= WsInvoke.excute(getUri(), map);
        //System.out.println(xml);
        return getResult(xml);
    }
    public static List<Map<String,String>> getResult(String xml) throws Exception {
        Map uris=new HashMap();
        uris.put("ns1", "http://service.tlm.webservice.creatsoft.com/");
        SAXReader reader=new SAXReader();
        Document document=reader.read(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        XPath xPath=document.createXPath("//return");
        xPath.setNamespaceURIs(uris);
        List<DefaultElement> nodes=xPath.selectNodes(document);
        String resultXml= nodes.get(0).getData().toString();
        document=reader.read(new ByteArrayInputStream(resultXml.getBytes("UTF-8")));
        String flag=document.getRootElement().element("flag").getData().toString();
        String msg=document.getRootElement().element("msg").getData().toString();
        List<Element> elements= document.getRootElement().element("table").elements();

        List<Map<String,String>> result=new ArrayList();
        if(elements.size()>0) {
            //获取table 里面的每个属性           ;
            List<String> atts = new ArrayList<>();
            for (int i = 0; i < elements.get(0).attributes().size(); i++) {
                atts.add(((DefaultAttribute) elements.get(0).attributes().get(i)).getName());
            }
            Map<String, String> map;
            for (Element element : elements) {
                map = new HashMap<>();
                for (int i = 0; i < atts.size(); i++) {
                    map.put(atts.get(i), element.attribute(atts.get(i)).getData().toString());
                }
                result.add(map);
            }
        }
        return  result;
    }
    /*<creatsoft>
<flag>1</flag>
<msg>成功</msg>
<table>
<row 分厂名称="勘探开发研究院" 车间名称="岩屑库" 装置名称="岩屑库" />*/

    public  void main(String[] args) throws  Exception{
        List<?> re= invokeService("<ser:getPipe> <arg0> &lt;creatsoft&gt;&lt;param name=\"管道编号\" value=\"TH-HF-NS-GC-0107\" /&gt;&lt;/creatsoft&gt;</arg0> </ser:getPipe>");
        System.out.println(re);
    }
}
